package com.gsj.web.controller.dept;

import com.github.pagehelper.PageInfo;
import com.gsj.domain.system.Dept;
import com.gsj.service.system.DeptService;
import com.gsj.web.basecrotonller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/dept") public class DeptController extends BaseController {
    @Autowired
    private DeptService deptService;

    @RequestMapping("/list")
    public String findByPage(@RequestParam(defaultValue = "1") int pageNum,
                             @RequestParam(defaultValue = "5") int pageSize
                             ){
        String companyId="1";
        PageInfo<Dept> byPage = deptService.findByPage(companyId, pageNum, pageSize);
        request.setAttribute("pageInfo",byPage);
        return "system/dept/dept-list";
    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        String companyId="1";
        List<Dept> deptList = deptService.findAll(companyId);
        request.setAttribute("deptList",deptList);
        return "system/dept/dept-add(1)";
    }

    @RequestMapping("/edit")
    public String edit(Dept dept){
        dept.setCompanyId("1");
        dept.setCompanyName("传智播客教育股份有限公司");
       if(StringUtils.isEmpty(dept.getId())){
           deptService.save(dept);
       }else {
            deptService.update(dept);
       }
            return "redirect:/system/dept/list";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(String id,Model model){
        Dept dept = deptService.findById(id);
        List<Dept> all = deptService.findAll("1");
       request.setAttribute("dept",dept);
        request.setAttribute("deptList",all);
        return "system/dept/dept-update";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Integer> delete(String id){
        Map<String,Integer> result=new HashMap<>();
        Boolean flag=deptService.delete(id);
        if(flag){
            result.put("message",1);
        }else {
            result.put("message",0);
        }
        return result;
    }
}
